//
//  CellForRadio.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "CellForRadio.h"
#import "ModelForAll.h"
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "CellForRadioList.h"
#import "NetworkingHandler.h"



@interface CellForRadio ()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, retain) NSMutableArray *arrCid;
@property (nonatomic, retain) NSMutableArray *arrTid;
@property (nonatomic, retain) NSMutableArray *arrCname;
@property (nonatomic, retain) NSMutableArray *arrTname;
@property (nonatomic, retain) NSMutableArray *arrTitle;
@property (nonatomic, retain) NSMutableArray *arrImgsrc;
@property (nonatomic, retain) NSMutableArray *arrPostid;

@property (nonatomic, retain) UITableView *tabView;


@end

@implementation CellForRadio



- (void)dealloc
{
    [super dealloc];
    
    [_arrCid release];
    
    [_arrTid release];
    
    [_arrCname release];
    
    [_arrTitle release];
    
    [_arrImgsrc release];
    
    [_arrPostid release];
    
    [_tabView release];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil)
    {  
        //创建子控件
        [self initialization];
        
        [self createTabView];
    
        self.tabView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self removeArrs];
            
            [self handleData];
        }];
    }
    return self;
}



#pragma mark 初始化
- (void)initialization
{
   [self handleData];
    
    self.arrCid = [NSMutableArray array];
    
    self.arrTid = [NSMutableArray array];
    
    self.arrCname = [NSMutableArray array];
    
    self.arrTname = [NSMutableArray array];
    
    self.arrTitle = [NSMutableArray array];
    
    self.arrImgsrc = [NSMutableArray array];
    
    self.arrPostid = [NSMutableArray array];
    
}


- (void)removeArrs
{
    [self.arrCid removeAllObjects];
    
    [self.arrTid removeAllObjects];
    
    [self.arrCname removeAllObjects];
    
    [self.arrTname removeAllObjects];
    
    [self.arrTitle removeAllObjects];
    
    [self.arrImgsrc removeAllObjects];
    
    [self.arrPostid removeAllObjects];
}








//collectionCell 的子控件的布局通常在此方法中设置
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
}






#pragma mark 自定义方法 >>>>>>
#pragma mark 创建TableView
- (void)handleData
{
    //设置访问地址
    NSString *url = @"http://c.3g.163.com/nc/topicset/ios/radio/index.html";
    
    [NetworkingHandler netWorkingWithURL:url completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
        ModelForAll *model = [[ModelForAll alloc] init];
        
        [model setValuesForKeysWithDictionary:result];
        
        [model release];
        
        for (NSDictionary *dic in model.cList)
        {
            NSString *str = [dic objectForKey:@"cid"];
            
            [self.arrCid addObject:str];
            
            NSString *str1 = [dic objectForKey:@"cname"];
            
            [self.arrCname addObject:str1];
            
            NSArray *arr = [dic objectForKey:@"tList"];
            
            for (NSDictionary *dic1 in arr)
            {
                NSString *str2 = [dic1 objectForKey:@"tname"];
                
                [self.arrTname addObject:str2];
                
                NSString * str5 = [dic1 objectForKey:@"tid"];
                
                [self.arrTid addObject:str5];
                
                NSDictionary *dic2 = [dic1 objectForKey:@"radio"];
                
                NSString *str3 = [dic2 objectForKey:@"title"];
                
                NSString *str4 = [dic2 objectForKey:@"imgsrc"];
                
                NSString *str6 = [dic2 objectForKey:@"postid"];
                
                [self.arrTitle addObject:str3];
                
                [self.arrImgsrc addObject:str4];
                
                [self.arrPostid addObject:str6];
            }
        }
            [self.tabView reloadData];
            
            [self.tabView.mj_header endRefreshing];
    }];

    
}



#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
    
    [self.contentView addSubview:self.tabView];
    
    [_tabView release];
    
    self.tabView.dataSource = self;
    
    self.tabView.delegate = self;
    
    [self.tabView registerClass:[CellForRadioList class] forCellReuseIdentifier:@"pool1"];
    
    //去掉分割线
     self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
}




#pragma mark UITableViewDataSource 协议方法
#pragma mark 相应分区的行的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrCname.count;
}



#pragma mark 给每行添加cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CellForRadioList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    //去掉选择cell之后背景颜色灰的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.arrCname = self.arrCname;
    
    cell.arrTitle = self.arrTitle;
    
    cell.arrTname = self.arrTname;
    
    cell.arrImgsrc = self.arrImgsrc;
    
    cell.arrPostid = self.arrPostid;
    
    cell.arrTid = self.arrTid;
    
    cell.arrCid = self.arrCid;
    
    cell.count = indexPath.row;
    
    cell.lableForCname.text = self.arrCname[indexPath.row];
    
    [cell.collection reloadData];
    
    return cell;
}



#pragma mark UITableViewDelegate 协议方法
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}

@end
